package com.kedou.sdk.assist;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.bytedance.applog.game.GameReportHelper;
import com.kedou.sdk.JuHaoWanSDK;
import com.kedou.sdk.JuHaoWanSDKCallback;
import com.kedou.sdk.core.HttpEngineApi;
import com.kedou.sdk.handler.SimpleAsyncResponseHandler;
import com.kedou.sdk.params.CheckOrderResponseParams;

/**
 * Created by Administrator on 2017/6/12 0012.
 */

public final class CallbackObservable implements Handler.Callback {

    private static final int MSG_CALL_EXCEPTION_WHAT = 0;
    private static final int MSG_CALL_RESPONSE_WHAT = 1;

    private volatile static CallbackObservable instance;

    private JuHaoWanSDKCallback mLoginCallbackObserver;
    private JuHaoWanSDKCallback mPaynCallbackObserver;
    private Handler mHandler;

    private CallbackObservable() {
        mHandler = new Handler(Looper.getMainLooper(), this);
    }

    public static CallbackObservable getInstance() {
        if (instance == null) {
            synchronized (CallbackObservable.class) {
                if (instance == null) {
                    instance = new CallbackObservable();
                }
            }

        }
        return instance;
    }

    public void setLoginCallbackObserver(JuHaoWanSDKCallback observer) {
        this.mLoginCallbackObserver = observer;
    }

    public void setPayCallbackObserver(JuHaoWanSDKCallback observer) {
        this.mPaynCallbackObserver = observer;
    }


    public void callExcetion(Constant.CallType callType, Exception ex) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("exception", ex);

        Message msg = mHandler.obtainMessage(MSG_CALL_EXCEPTION_WHAT, callType);
        msg.setData(bundle);
        mHandler.sendMessage(msg);
    }

    public void callResponseResult(Constant.CallType callType, int code, String msg, String result) {
        Bundle bundle = new Bundle();
        bundle.putInt("code", code);
        bundle.putString("message", msg);
        bundle.putString("result", result);

        Message handerMsg = mHandler.obtainMessage(MSG_CALL_RESPONSE_WHAT, callType);
        handerMsg.setData(bundle);
        mHandler.sendMessage(handerMsg);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_CALL_RESPONSE_WHAT:
                int code = msg.getData().getInt("code");
                String message = msg.getData().getString("message");
                String result = msg.getData().getString("result");
                callObserverResponseResult((Constant.CallType) msg.obj, code, message, result);
                break;

            case MSG_CALL_EXCEPTION_WHAT:
                Exception ex = (Exception) msg.getData().getSerializable("exception");
                callObserverExcetion((Constant.CallType) msg.obj, ex);
                break;

            default:
        }

        return false;
    }



    void callObserverExcetion(Constant.CallType callType, Exception ex){
        switch (callType) {
            case CALL_PAY:
                if (mPaynCallbackObserver != null) {
                    mPaynCallbackObserver.onWanSDKException(ex);
                }
                break;

            case CALL_LOGIN:
                if (mLoginCallbackObserver != null) {
                    mLoginCallbackObserver.onWanSDKException(ex);
                }
            default:
        }
    }


    void callObserverResponseResult(Constant.CallType callType, int code, String msg, String result) {
        switch (callType) {
            case CALL_PAY:
                if (mPaynCallbackObserver != null) {
                    mPaynCallbackObserver.onWanSDKResponse(code, msg, result);
                }
                HttpEngineApi.getInstance().checkOrder(HttpEngineApi.getInstance().getEngineConduits().readCurrentOrder(),
                        JuHaoWanSDK.getInstance().getToutiaoAppid(),new SimpleAsyncResponseHandler<CheckOrderResponseParams>(CheckOrderResponseParams.class) {
                    @Override
                    public void onAsyncResponseSuccess(int flag, CheckOrderResponseParams response) {
                        if(response != null && response.isSuccess()&& JuHaoWanSDK.getInstance().isToutiaoInit()){
                            GameReportHelper.onEventPurchase(response.getData().getGoods_name(), response.getData().getGoods_name(), "1", 1, "未知", "¥", true, (int)response.getData().getAmount_paid());
                        }
                    }
                });
                break;

            case CALL_LOGIN:
                if (mLoginCallbackObserver != null) {
                    mLoginCallbackObserver.onWanSDKResponse(code, msg, result);
                    openWindowContent();
                }
            default:

        }
    }

    private void openWindowContent(){
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                JuHaoWanSDK.getInstance().openWindowContent();
            }
        }, 1000);

    }

}
